﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class BrowserKSB : System.Web.UI.Page
{
    private PiLink.declare PIDeclare = null;
    SAC.Elink.Elink ek = new SAC.Elink.Elink();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Timer1.Interval = 3000;
            Bind();
        }
    }

    public void Bind()
    {
        this.grvFeeInfo.DataSource = GetAlarmInfo();
        this.grvFeeInfo.DataBind();

    }
    public System.Collections.ArrayList GetAlarmInfo()
    {
        System.Collections.ArrayList al = new ArrayList();
        Bussiness.KBS kbs = new Bussiness.KBS();
        System.Data.DataTable dt = kbs.GetAlarmInfo("指标");

        try
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (System.Data.DataRow ddr in dt.Rows)
                {
                    string paraID = ddr[0].ToString();
                    DataTable ddt = kbs.GetAlarm(paraID);
                    if (ddt != null && ddt.Rows.Count > 0)
                    {
                        foreach (System.Data.DataRow dr in ddt.Rows)
                        {
                            Bussiness.KBS Ckbs = new Bussiness.KBS();
                            Ckbs.AlarmDESC = dr[1].ToString();
                            Ckbs.Operation = dr[4].ToString();

                            string piname = dr[5].ToString();
                            if (PIDeclare == null)
                                InitLink();
                            Bussiness.PIModel model = new Bussiness.PIModel();
                            double d = ek.GetRTValue(piname); //model.GetCurrentValue(piname, PIDeclare);
                            Ckbs.PIValue = d.ToString("0.00");

                            Bussiness.RelationAlarm rAlarm = new Bussiness.RelationAlarm();
                            System.Data.DataTable cdt = rAlarm.GetDescription(dr[1].ToString());
                            System.Text.StringBuilder build = new System.Text.StringBuilder();
                            if (cdt != null && cdt.Rows.Count > 0)
                            {
                                for (int j = 0; j < cdt.Rows.Count; j++)
                                {
                                    int k = j + 1;
                                    build.Append(k.ToString() + "、");
                                    double cv = ek.GetRTValue(cdt.Rows[j][0].ToString()); //model.GetCurrentValue(cdt.Rows[j][0].ToString(), PIDeclare);
                                    build.Append(cdt.Rows[j][1].ToString());
                                    build.Append("：" + cv.ToString("0.00"));
                                    build.Append("<br />");

                                }
                            }
                            Ckbs.RelationDESC = build.ToString();
                            al.Add(Ckbs);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return al;
    }

    private void InitLink()
    {
        string server = ConfigurationManager.ConnectionStrings["server"].ToString();
        string piuser = ConfigurationManager.ConnectionStrings["piuser"].ToString();
        string password = ConfigurationManager.ConnectionStrings["password"].ToString();

        DB2Provider.PIBaseLink link = new DB2Provider.PIBaseLink();
        link.ServerName = server;
        link.UserName = piuser;
        link.PassWord = password;

        PIDeclare = link.GetPI();
    }
    protected void grvFeeInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvFeeInfo.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Bind();
    }
}
